/*
 * PlaylistRandomAction.java
 *
 * Created on December 6, 2005, 12:24 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.lib.actions;

import com.bff.mpd.utils.Utils;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.bff.javampd.MPDPlayer;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDPlayerException;

/**
 *
 * @author Bill Findeisen
 */
public class PlaylistRandomAction extends AbstractAction {

    private MPDPlayer player;
    private boolean random;

    /** Creates a new instance of PlaylistRandomAction */
    public PlaylistRandomAction(MPDPlayer player) {
        this.player = player;
        setName();
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        try {
            player.setRandom(!isRandom());
            setName();
        } catch (MPDConnectionException ex) {
            Logger.getLogger(PlaylistRandomAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDPlayerException ex) {
            Logger.getLogger(PlaylistRandomAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void setName() {
        try {
            if (player.isRandom()) {
                setRandom(true);
                putValue(Action.NAME, Utils.getMessage("ACTION_RANDOM_UNRANDOM_NAME"));
            } else {
                setRandom(false);
                putValue(Action.NAME, Utils.getMessage("ACTION_RANDOM_RANDOM_NAME"));
            }
        } catch (MPDConnectionException ex) {
            Logger.getLogger(PlaylistLoopAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDPlayerException ex) {
            Logger.getLogger(PlaylistLoopAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /**
     * @return the random
     */
    public boolean isRandom() {
        return random;
    }

    /**
     * @param random the random to set
     */
    public void setRandom(boolean random) {
        this.random = random;
    }
}
